﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmHoaDonSanPham : Form
    {
        public frmHoaDonSanPham()
        {
            InitializeComponent();
        }

        private void panelEx1_Click(object sender, EventArgs e)
        {

        }

        private void frmHoaDonSanPham_Load(object sender, EventArgs e)
        {
            try
            {
                HoaDonSanPhamBUS hdspbus = new HoaDonSanPhamBUS();
                List<HoaDonSanPhamDTO> ds_hdsp = new List<HoaDonSanPhamDTO>();
                ds_hdsp = hdspbus.LayDanhSachHoaDonSanPham();
                int stt = 1;
                for (int i = 0; i < ds_hdsp.Count; i++)
                {
                   
                    dgvDanhSachDDH.Rows.Add(stt, ds_hdsp[i].MaHDSP, ds_hdsp[i].NgayLap, ds_hdsp[i].TongTien, ds_hdsp[i].MaKH, ds_hdsp[i].TenKH);
                    stt++;
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show("Lỗi:" + ex.ToString());
            }
         
        }

        private void dgvDanhSachDDH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            int intIndex = e.RowIndex;
           
            dgvDanhSachDDH.Rows[intIndex].Selected = true;

           
            
            try
            {
                dgvChiTietDDH.Rows.Clear();
                  int ma = int.Parse(dgvDanhSachDDH.Rows[e.RowIndex].Cells[1].Value.ToString());
                    hienthichitiet_PXK(ma);
            }
            catch (Exception )
            {
                
                return;
            }
          
        }

        public void hienthichitiet_PXK(int ma)
        {
            CT_HoaDonSanPhamBUS cthdsp = new CT_HoaDonSanPhamBUS();
            List<CT_HoaDonSanPhamDTO> dscthdsp = new List<CT_HoaDonSanPhamDTO>();
            dscthdsp = cthdsp.LayDanhSachHoaDonSanPham(ma);

          

            int stt = 1;
            for (int i = 0; i < dscthdsp.Count; i++)
            {
                dgvChiTietDDH.Rows.Add(stt, dscthdsp[i].MaNVL, dscthdsp[i].TenNVL, dscthdsp[i].SoLuong, dscthdsp[i].DonViTinh);
            }

        }

        private void btnThem_Click(object sender, EventArgs e)
        {

            frmThemHoaDonSanPham fr = new frmThemHoaDonSanPham();
          
            fr.ShowDialog();
            
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
